package net.lovesrc.support.service.mobileverifycode;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.Requests;
import com.zhouzhou.fastjava.util.StringKit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 嘎嘎短信验证码平台
 */
public class NoNameMobileVerifyCodeService extends AbstractMobileVerifyCodeService implements MobileVerifyCodeService {

    private static final Logger logger = LoggerFactory.getLogger(NoNameMobileVerifyCodeService.class);

    public static final String DEFAULT_HOST = "http://api.jmyzm.com";

    private String username;

    private String password;

    private String gatewayUrl;

    public NoNameMobileVerifyCodeService(String host, String username, String password) {
        this.username = username;
        this.password = password;
        this.gatewayUrl = host + "/api";
    }

    @Override
    protected String doGetVerifyCodeFromPhone(String itemId, String phone) {
        JSONObject params = new JSONObject();
        params.put("id", itemId);
        params.put("token", getToken());
        params.put("phone", phone);
        params.put("t", username);
        String body = Requests.get(gatewayUrl + "/yh_qm/")
                .params(params)
                .execute()
                .asText();
        if (body.startsWith("1|")) {
            return StringUtils.substringAfter(body, "1|");
        }
        return null;
    }

    @Override
    public void addBlack(String itemId, String phone) {
        //自动加黑
    }

    @Override
    public void releasePhone(String itemId, String phone) {
        // 自动释放
    }

    @Override
    /**
     *  [传入参数]：
     * [1] id=项目ID (对应的项目ID可在客户端软件里查询)
     * [2] operator=0 (0=默认 1=移动 2=联通 3=电信)
     * [3] Region=0 (0=默认)需要哪个地区的直接输入如：上海 系统会自动筛选上海的号码
     * [4] card=0 (0=默认 1=虚拟运营商 2=实卡)
     * [5] phone= (你要指定获取的号码,不传入号码情况下,获取新号码.)
     * [6] loop=1（1=过滤 2=不过滤）1排除已做过号码取号时不会再获取到，2不过滤已做号码可以取号时循环获取号码（号码循环做业务必须选择2）
     * [7] token=(登录成功返回的token)
     */
    public String getPhone(String itemId, String phone, String card) {

        JSONObject params = new JSONObject();
        params.put("id", itemId);
        params.put("operator", "0");
        params.put("Region", "0");
        params.put("token", getToken());
        params.put("card", card);
        params.put("phone", phone);
        params.put("loop", 1);
        String body = Requests.get(gatewayUrl + "/yh_qh/")
                .params(params)
                .execute()
                .asText();
        if (body.startsWith("1|")) {
            return StringUtils.substringAfter(body, "1|");
        } else {
            throw BizException.messageOf("获取手机号失败");
        }
    }

    @Override
    protected String login() {
        String body = Requests.get(gatewayUrl + "/sign/username=" + username + "&password=" + password)
                .execute()
                .asText();
        if (body.startsWith("1|")) {
            return StringUtils.substringAfter(body, "1|");
        } else {
            throw BizException.messageOf("登录失败");
        }
    }

}
